<?php
require_once APP . '/application/admincontrollers/AdminAction.php';
/**
 * 视频播放
 * @package admin
 */
class VideoController extends AdminAction
{
    
	/**
	 * Enter description here...
	 *
	 * @var MeetingVedio
	 */
	private $meetingvideo;
    public function init()
    {
    	require_once(APP . '/application/models/Video.php');
    	
        $this->video = new Video($this->db);
    }


    /**
     * 加载框架页
     */
    public function addAction()
    {
    	$this->view->updir = APP . '/upload/flv';
        //加载默认视图
    }
    
    public function doaddAction()
    {
    	$params = $this->getAllParams();
    	
    	/**************图片处理*********************/
    	$image = "";
    	if (!empty($_FILES) && !empty($_FILES['image']) && !empty($_FILES['image']['name'])){
    		$file_ext = pathinfo($_FILES['image']['name'], PATHINFO_EXTENSION);
    		$todir = APP . '/upload';
    		if (!file_exists($todir . '/image'))
    			mkdir($todir . '/image');
    		if (!file_exists($todir . '/image/' . date("Ymd")))
    			mkdir($todir . '/image/' . date("Ymd"));
    		
    		$image = '/upload/image/' . date("Ymd") . '/' .date("YmdHis") . '_' . uniqid("h") . '.' . $file_ext;
    		move_uploaded_file($_FILES['image']['tmp_name'], APP . $image);
    	}
    	$params['image'] = $image;
    	
    	$vid = $this->video->add($params);
    	
    	   	
    	$this->view->msg = '添加成功';
    	$this->view->url = $this->url($this->view->baseUrl, "video", "add", "", "admin");
    	$this->showmessage();
    }
    
    public function listAction()
    {
    	$nowp = (int) $this->getParam("nowp", 1);
    	$per_page = 20;
    	
    	$result = $this->video->listvideo($nowp, $per_page);
    	
    	$this->view->nowp = $nowp;
        $this->view->list = $result['list'];
        $this->view->page = $result['page'];
    }
    
    
    public function delAction()
    {
    	$mid = (int) $this->getParam("vid");
    	$nowp = (int) $this->getParam("nowp");
    	
    	$content = $this->video->show($mid);
    	$this->video->del($mid);
    	
    	header("location:" . $this->url($this->view->baseUrl, "video", "list", "nowp=" . $nowp, "admin"));
    }
    
    public function editAction()
    {
    	$vid = (int) $this->getParam("vid");
    	$nowp = (int) $this->getParam("nowp");
    	
        $this->view->nowp = $nowp;
        $this->view->content = $this->video->show($vid);
    }
    
    public function doeditAction()
    {
    	$vid = (int)$this->getParam("vid");
    	$nowp = (int)$this->getParam("nowp");
    	$params = $this->getAllParams();
    	
    	/**************图片处理*********************/
    	$image = "";
    	if (!empty($_FILES) && !empty($_FILES['image']) && !empty($_FILES['image']['name'])){
    		$file_ext = pathinfo($_FILES['image']['name'], PATHINFO_EXTENSION);
    		$todir = APP . '/upload';
    		if (!file_exists($todir . '/image'))
    			mkdir($todir . '/image');
    		if (!file_exists($todir . '/image/' . date("Ymd")))
    			mkdir($todir . '/image/' . date("Ymd"));
    		
    		$image = '/upload/image/' . date("Ymd") . '/' .date("YmdHis") . '_' . uniqid("h") . '.' . $file_ext;
    		move_uploaded_file($_FILES['image']['tmp_name'], APP . $image);
    	}
    	if (!empty($image))
    		$params['image'] = $image;
    	else 
    		unset($params['image']);
    	
    	$this->video->change($vid, $params);
    	$content = $this->video->show($vid);
    	
    	
    	$this->view->msg = '编辑成功';
    	$this->view->url = $this->url($this->view->baseUrl, "video", "list", "nowp=" . $nowp, "admin");
    	$this->showmessage();
    }
}
